﻿//-----------------------------------------------------------------------------
// <copyright file="ServiceProviderExtensions.cs" company="William E. Kempf">
//     Copyright (c) William E. Kempf.
// </copyright>
//-----------------------------------------------------------------------------

namespace Onyx.ComponentModel
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Provides extension methods for <see cref="IServiceProvider"/>.
    /// </summary>
    public static class ServiceProviderExtensions
    {
        /// <summary>
        /// Gets the service object of the specified type.
        /// </summary>
        /// <typeparam name="T">Type of service object to get.</typeparam>
        /// <param name="self">An <see cref="IServiceProvider"/> instance.</param>
        /// <returns>
        /// A service object of type <typeparamref name="T"/>.
        /// -or-
        /// null if there is no service object of type <typeparamref name="T"/>.
        /// </returns>
        /// <exception cref="ObjectDisposedException">This instance has been disposed.</exception>
        /// <summary>
        /// Gets the service.
        /// </summary>
        [SuppressMessage("Microsoft.Design",
            "CA1004:GenericMethodsShouldProvideTypeParameter",
            Justification = "The whole point is to provide an easier to use typesafe overload.")]
        public static T GetService<T>(this IServiceProvider self)
        {
            return (T)self.GetService(typeof(T));
        }
    }
}
